﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.Framework.ProviderLoader;
using System.Configuration;
using PSE.Framework.Configuration;
using PSE.Framework.ProviderLoader.Configuration;

namespace PSE.Framework.Service.ExceptionHandling
{
    internal class ErrorMessageReaderProviderFactory
    {
        private const string CONFIG_SECTION_NAME = "serviceProviders";
        private const string PROVIDER_NAME = "ErrorMessageReaderProvider";

        /// <summary>
        /// Retorna o provider de Autenticação
        /// </summary>
        /// <returns>Instância do provider de autenticação configurado.</returns>
        public static IErrorMessageReaderProvider RetrieveProvider()
        {
            try
            {
                ProviderConfigurationSection configSection = ApplicationConfiguration.GetCurrent().GetSection<ProviderConfigurationSection>(CONFIG_SECTION_NAME);

                if (configSection != null)
                {
                    return ProviderLoader<IErrorMessageReaderProvider>.LoadProvider(configSection.Providers, PROVIDER_NAME);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                //System.Diagnostics.EventLog.WriteEntry("ErrorMessageReaderProviderFactory", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                throw new ConfigurationErrorsException("Could not create an instance of Error Message Reader Provider configured.", ex);
            }
        }
    }
}
